Ext.define('cp.controller.Admin',{
    extend: 'Ext.app.Controller',
    
    views: [
            'ui.ToolbarAdmin',
            'ui.DecoratorForm',
            'ui.DecoratorFormSimple',
            'form.PdfTurnos',
            'grid.GestionBase',
            'grid.GestionSecretaria',
            'form.SecretariaBase',
            'form.AltaSecretaria',
            'form.EditSecretaria',
            'form.BuscarSecretaria',
            'grid.GestionAdmins',
            'form.AdminBase',
            'form.AltaAdmin',
            'form.VerAdmin',
            'form.EditAdmin',
            'form.BuscarAdmin'
           ],
    model: [
        'grid.Paciente',
        'form.Secretaria'
    ],
    store: [
        'grid.Secretaria',
        'form.Secretaria',
        'form.SecretariasRiltrados',
        'grid.Admins',
        'form.Admin',
        'grid.AdminsFiltrados'
        
    ],
    
    init: function(){
    
        this.control({
            
          'undeleteConfirm button[text=Si]':{
                click: function(btn){
                    var grid = btn.up('undeleteConfirm').target;
                    var id = grid.filaSeleccionada().get(grid.idProperty);
                    grid.recuperarItemPorId(id);
                }
            },

            'deleteConfirm button[text=Si]':{
                click: function(btn){
                    var grid = btn.up('deleteConfirm').target;
                    var id = grid.filaSeleccionada().get(grid.idProperty);
                    grid.borrarItemPorId(id);
                }
            },
            
            'decoratorFormSearch button[action=find]':{
                click: function(btn){
                    btn.up('decoratorFormSearch').submitForm();
                }
            },
            'decoratorFormSearch button[action=clear]':{
                click: function(btn){
                    btn.up('decoratorFormSearch').clearForm();
                }
            },
            
            'toolbarAdmin button[action=gestion] menuitem[action=turnosDelDia]':{
                click: function(){
                    var t = "PDF turnos de la fecha";
                    var f = Ext.create('cp.view.form.PdfTurnos',{});
                    var w = Ext.create('cp.view.ui.DecoratorFormSimple',{
                        aTitle: t,
                        aForm: f
                    });
                    w.show();
                }
            },
            'toolbarAdmin button[action=sesion] menuitem[action=logout]':{
                click: function(){
                    window.location = 'bin/session/logout.php'
                }
            },
            // Submit del PDF
            'decoratorFormSimple button[action=aceptar]':{
                click: function(btn){
                    btn.up('decoratorFormSimple').submitForm();
                }
            },
            // Muestro la grid de Secretarias
            'toolbarAdmin button[action=gestion] menuitem[action=secretaria]':{
                click: function(){
                    var store = Ext.create('cp.store.grid.Secretaria',{});
                    var grid = Ext.create('cp.view.grid.GestionSecretaria',{
                        dataStore: store
                    });
                    var win = Ext.create('cp.view.ui.DecoratorGrid',{
                        aTitle: 'Gestión de secretarias',
                        aGrid: grid
                    });
                    win.show();
                }
            },
            // Muestro el form de alta secretaria
            'gestionSecretaria button[action=add]':{
                click: function(btn){
                    btn.up('gestionSecretaria').verFormularioAlta();
                }
            },
            'gestionSecretaria button[action=delete]':{
                click: function(btn){
                    btn.up('gestionSecretaria').borrarItem();
                }
            },
            'gestionSecretaria button[action=undelete]':{
                click: function(btn){
                    btn.up('gestionSecretaria').recuperarItem();
                }
            },
            'gestionSecretaria button[action=edit]':{
                click: function(btn){
                    btn.up('gestionSecretaria').verFormularioEdicion();
                }
            },
            'gestionSecretaria button[action=show]':{
                click: function(btn){
                    btn.up('gestionSecretaria').verFormularioVista();
                }
            },
            'decoratorForm button[action=save]':{
                click: function(btn){
                    btn.up('decoratorForm').submitForm();
                }
            },
            'decoratorForm button[action=cancel]':{
                click: function(btn){
                    btn.up('decoratorForm').cancelar();
                }
            },
            'gestionSecretaria button[action=search]':{
                click: function(btn){
                    btn.up('gestionSecretaria').verFormularioBusqueda();
                }
            },
            
            // Muestro la grid de Admins
            'toolbarAdmin button[action=gestion] menuitem[action=admins]':{
                click: function(){
                    var store = Ext.create('cp.store.grid.Admins',{});
                    var grid = Ext.create('cp.view.grid.GestionAdmins',{
                        dataStore: store
                    });
                    var win = Ext.create('cp.view.ui.DecoratorGrid',{
                        aTitle: 'Gestión de Administradores',
                        aGrid: grid
                    });
                    win.show();
                }
            },
            'gestionAdmins button[action=add]':{
                click: function(btn){
                    btn.up('gestionAdmins').verFormularioAlta();
                }
            },

            'gestionAdmins button[action=delete]':{
                click: function(btn){
                    btn.up('gestionAdmins').borrarItem();
                }
            },
            'gestionAdmins button[action=undelete]':{
                click: function(btn){
                    btn.up('gestionAdmins').recuperarItem();
                }
            },
            'gestionAdmins button[action=show]':{
                click: function(btn){
                    btn.up('gestionAdmins').verFormularioVista();
                }
            },
            'gestionAdmins button[action=edit]':{
                click: function(btn){
                    btn.up('gestionAdmins').verFormularioEdicion();
                }
            },
            'gestionAdmins button[action=search]':{
                click: function(btn){
                    btn.up('gestionAdmins').verFormularioBusqueda();
                }
            }
            
        });
    }
});